package com.boyon.system.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.boyon.common.exection.BusinessException;
import com.boyon.common.log.annotation.MethodFlag;
import com.boyon.common.status.LimitType;
import com.boyon.common.tools.ObjectTool;
import com.boyon.hbm.criteria.Query;
import com.boyon.service.AbstractService;
import com.boyon.system.entity.Config;

/**
 * ClassName:ConfigService <br/>
 * Function: 系统配置信息业务处理类. <br/>
 * Date: 2018年3月14日 下午10:31:17 <br/>
 * 
 * @author sunshulin
 * @version
 * @since JDK 1.8
 * @see
 */
@Service
public class ConfigService extends AbstractService {

	/**
	 * 
	 * modifyConfig:修改配置信息内容. <br/>
	 * 
	 * @author sunshulin
	 * @param config
	 * @return
	 * @since JDK 1.8
	 */
	@MethodFlag(key = "")
	@Transactional
	public Long modifyConfig(Config config) {
		Config entity = findByConfigId(config.getConfigId());
		if (entity.getUpdateLimit() == LimitType.one.getVal()
				&& entity.getCounting() > LimitType.one.getVal()) {
			throw BusinessException.builder(0, "信息只能被修改一次");
		}
		entity.setConfigValue(config.getConfigValue());
		entity.setCounting(ObjectTool.isNotNull(entity.getCounting())
				? entity.getCounting() + 1
				: 1);
		generalDao.saveOrUpdate(entity);
		return config.getConfigId();
	}

	/**
	 * 
	 * findById:配置信息ID查询配置信息内容. <br/>
	 * 
	 * @author sunshulin
	 * @param configId
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional(readOnly = true)
	public Config findByConfigId(Long configId) {
		Config entity = generalDao.get(Config.class, configId);
		if (entity == null) {
			throw BusinessException.builder(0, "配置信息不存在");
		}
		return entity;
	}

	/**
	 * 
	 * query:查询配置列表. <br/>
	 * 
	 * @author sunshulin
	 * @param query
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional(readOnly = true)
	public List<Config> query(Query query) {
		List<Config> list = generalDao.query(Config.class, query);
		return list;
	}
}
